//
//  FZTabBarController.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/3/26.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit
import SVProgressHUD

class FZTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

     addChildViewController(childController: FZHomeController(), imgName: "tabbar_home", title: "首页")
     addChildViewController(childController: FZMessageController(), imgName: "tabbar_message_center", title: "消息")
     addChildViewController(childController: FZDiscoverController(), imgName: "tabbar_discover", title: "搜索")
     addChildViewController(childController: FZMineController(), imgName: "tabbar_profile", title: "我的")
        
     let tabbar = FZTabBar()
        
        
     tabbar.flagClosure = { [weak self] in
        
        if FZUserViewModel.sharedUser.islogin == false{
           SVProgressHUD.showError(withStatus: "未登录,亲😋")
         
           SVProgressHUD.dismiss(withDelay: 1.0)
            
           return
        }
        
            let coverLayer = FZCoverLayerView()

            self?.view.addSubview(coverLayer)
        
        coverLayer.flagClosure = { [weak self] (className : String) in
            
            let nameVC = NSClassFromString(className) as! UIViewController.Type
            
            let sendBlogVC = nameVC.init()
            
            let sendBlogNav = FZNavigationController(rootViewController: sendBlogVC)
            //跳转控制器
            self?.present(sendBlogNav, animated: true, completion: nil)
            
          }

        }
        
        setValue(tabbar, forKey: "tabBar")
        
    }

   private func addChildViewController(childController: UIViewController , imgName: String,title: String) {
        
        childController.tabBarItem.image = UIImage.init(named: imgName)
        childController.tabBarItem.selectedImage = UIImage.init(named: imgName + "_selected")?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.boldSystemFont(ofSize: 12)], for: .normal)
        
        childController.title = title
        
        
        let nav = FZNavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
    }
}
